package own.stu.jobgib.playown;

import com.google.common.base.Objects;
import lombok.Data;
import org.springframework.core.io.FileSystemResource;
import org.springframework.util.ResourceUtils;

import java.io.*;
import java.util.*;
import java.util.stream.Collectors;

public class FundSort {

    private static int interval = 4;

    public static void main(String[] args) throws IOException {
        Map<String, List<String>> lineMap = getLines();

        Map<String, List<Node>> result = new HashMap<>();

        lineMap.entrySet().forEach(v -> {

            List<String> lines = v.getValue();

            if (lines.size() % interval != 0) {
                return;
            }

            if(v.getKey().contains("5")){
                return;
            }

            int nums = lines.size() / interval;
            List<Node> funds = new ArrayList<>();
            for (int i = 0; i < nums; i++) {
                Node fund = new Node();
                for (int j = 0; j < interval; j++) {
                    lines.get(i * interval + j);
                }
                fund.setName(lines.get(i * interval + 0));
                fund.setNetWorth(lines.get(i * interval + 1));
                fund.setIncrease(lines.get(i * interval + 2));
                fund.setCode(lines.get(i * interval + 3));

                funds.add(fund);
                funds.sort((a, b) -> b.getIncrease().compareTo(a.getIncrease()));

                result.put(v.getKey(), funds);
            }
            System.out.println(funds);
        });


        /*Set<Node> all = new HashSet<>();
        Collection<List<Node>> values = result.values();
        Iterator<List<Node>> iterator = values.iterator();
        while (iterator.hasNext()){
            List<Node> next = iterator.next();
            if(all.isEmpty()){
                all.addAll(next);
            }
            all.retainAll(next);
        }
        System.out.println(all);*/

        List<Node> all = new ArrayList<>();
        Collection<List<Node>> values = result.values();
        Iterator<List<Node>> iterator = values.iterator();
        while (iterator.hasNext()){
            List<Node> next = iterator.next();
            all.addAll(next);
        }

        Map<String, List<Node>> collect = all.stream().collect(Collectors.groupingBy(Node::getCode));
        System.out.println(collect);
    }

    @Data
    public static class Node {
        String code;
        String name;
        String netWorth;
        String increase;

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            Node node = (Node) o;
            return Objects.equal(code, node.code);
        }

        @Override
        public int hashCode() {
            return Objects.hashCode(code);
        }
    }

    private static Map<String, List<String>> getLines() throws IOException {

        String path = "C:\\Users\\chen\\Desktop\\fund";
        File dir = new File(path);

        Map<String, List<String>> map = new HashMap<>();

        if (dir.isDirectory()) {
            File[] files = dir.listFiles();
            Arrays.stream(files).forEach(v -> {
                try {
                    FileSystemResource resource = new FileSystemResource(v.getPath());

                    InputStream in = null;

                    in = resource.getInputStream();

                    List<String> lines = new ArrayList<>();
                    BufferedReader reader = new BufferedReader(new InputStreamReader(in));
                    while (true) {
                        String line = reader.readLine();
                        if (line == null) break;
                        lines.add(line);
                    }
                    reader.close();

                    map.put(v.getName(), lines);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            });
        }

        return map;
    }
}
